package org.dromara.mp.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mp.annotation.SwitchoverCheck;
import org.dromara.mp.service.WxMpMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 微信公众号菜单管理控制器
 *
 * @author ruoyi
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/mp/menu/{appid}")
public class WxMpMenuController extends BaseController {

    private final WxMpService wxMpService;
    private final WxMpMenuService wxMpMenuService;

    /**
     * 创建菜单
     *
     * @param appid  公众号appid
     * @param wxMenu 菜单信息
     * @return 创建结果
     */
    @PostMapping("/create")
    @SwitchoverCheck
    public R<Void> createMenu(@PathVariable String appid, @RequestBody WxMenu wxMenu) {
        try {
            boolean result = wxMpMenuService.createMenu(wxMenu);
            return result ? R.ok() : R.fail("创建菜单失败");
        } catch (Exception e) {
            log.error("创建菜单失败: {}", e.getMessage(), e);
            return R.fail("创建菜单失败: " + e.getMessage());
        }
    }

    /**
     * 删除菜单
     *
     * @param appid 公众号appid
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @SwitchoverCheck
    public R<Void> deleteMenu(@PathVariable String appid) {
        try {
            boolean result = wxMpMenuService.deleteMenu();
            return result ? R.ok() : R.fail("删除菜单失败");
        } catch (Exception e) {
            log.error("删除菜单失败: {}", e.getMessage(), e);
            return R.fail("删除菜单失败: " + e.getMessage());
        }
    }

    /**
     * 查询菜单
     *
     * @param appid 公众号appid
     * @return 菜单信息
     */
    @GetMapping("/get")
    @SwitchoverCheck
    public R<WxMpMenu> getMenu(@PathVariable String appid) {
        try {
            WxMpMenu menu = wxMpMenuService.getMenu();
            return R.ok(menu);
        } catch (Exception e) {
            log.error("查询菜单失败: {}", e.getMessage(), e);
            return R.fail("查询菜单失败: " + e.getMessage());
        }
    }

    /**
     * 创建默认菜单
     *
     * @param appid 公众号appid
     * @return 创建结果
     */
    @PostMapping("/create-default")
    @SwitchoverCheck
    public R<Void> createDefaultMenu(@PathVariable String appid) {
        try {
            boolean result = wxMpMenuService.createDefaultMenu();
            return result ? R.ok() : R.fail("创建默认菜单失败");
        } catch (Exception e) {
            log.error("创建默认菜单失败: {}", e.getMessage(), e);
            return R.fail("创建默认菜单失败: " + e.getMessage());
        }
    }

    /**
     * 处理菜单点击事件
     *
     * @param appid    公众号appid
     * @param eventKey 事件key
     * @param openid   用户openid
     * @return 处理结果
     */
    @PostMapping("/handle-click")
    @SwitchoverCheck
    public R<String> handleMenuClick(@PathVariable String appid,
                                     @RequestParam String eventKey,
                                     @RequestParam String openid) {
        try {
            String response = wxMpMenuService.handleMenuClick(eventKey);
            return R.ok(response);
        } catch (Exception e) {
            log.error("处理菜单点击事件失败: {}", e.getMessage(), e);
            return R.fail("处理菜单点击事件失败: " + e.getMessage());
        }
    }
}
